<?php
	class Arsip_model extends Model{
		function Arsip_model(){
			parent::Model();
		}

		function list_header($ro=false) {
			if ($this->session->userdata('role')==GROUP_ADMIN) {
	            $s = "<table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tbl\">".
	                 "<thead><tr><th>No</th><th>Judul</th><th>File</th><th>Kategori</th><th>Tanggal</th><th>Viewer</th>".
	                 "<th>Published</th><th>Ukuran</th><th>Hits</th>";
			} else {
	            $s = "<table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tbl\">".
	                 "<thead><tr><th>No</th><th>Judul</th><th>File</th><th>Kategori</th><th>Tanggal</th><th>Viewer</th>".
	                 "<th>Published</th><th>Ukuran</th><th>Hits</th>";
			}
            if ($ro==false) {
                $s .= "<th colspan=2> Aksi </th>";
            }
            $s .= "</tr></thead><tbody>";
            return $s;
		}
		
        function list_isi($where="", $order="", $ro=false) {
        	$this->db->where(array('pta_id_kategori_arsip !=' => DISPOSISI));
        	if ($this->session->userdata('role') != GROUP_ADMIN) {
        		$this->db->where(array('viewer <=' => $this->session->userdata('role')));
        	}
        	if ($where!='') {
                $this->db->where($where);
            }
            if ($order == "") {
                $this->db->order_by('tgl_dokumen desc');
            } else {
                $this->db->order_by($order);
            }
            $q = $this->db->get('arsip');

            $s = "[";
			$i = 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
                if ($i % 2 == 1) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }

                $kategori = Datum_model::getData('nama', 'kategori_arsip', array('idpta_kategori_arsip' => $row->pta_id_kategori_arsip));
                $namafile = Datum_model::getData('namafile', 'files', array('idpta_files' => $row->pta_id_files));
                $ukuran_file = Datum_model::getData('ukuran', 'files', array('idpta_files' => $row->pta_id_files));
                
                $s .= "['<tr class=\"$kelas\" id=\"row-".$row->idpta_arsip."\" ><td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>".
                      "<td>".str_replace($order, $replace, $row->judul)."</td>".
                      "<td>".$namafile."</td>".
                      "<td>".$kategori."</td>".
                      "<td><div align=\"center\">".YMD2Indo(trimTime($row->tgl_dokumen), TGL_SEPARATOR )."</div></td>";
                if ($this->session->userdata('role')==GROUP_ADMIN) {
                	switch ($row->kode_viewer) {
                		case VIEWER_EVERYBODY: $viewer = 'Semua'; break;
                		case VIEWER_GRUP: $grup = Datum_model::getData('nama', 'grup', array('idpta_grup' => $row->viewer));
                			              $viewer = 'Grup '.$grup; break;
                		case VIEWER_USER_TTT: $viewer = 'User '.$row->viewer;
                	}
                	$s .= "<td><div align=\"center\">".$viewer."</div></td>";
                }
                $s .= "<td><div align=\"center\">".($row->published==1?ikonCeklist():ikonCakra())."</div></td>".
                      "<td><div align=\"center\">".$ukuran_file." kB</div></td>".
                      "<td><div align=\"center\">".$row->hits."</div></td>";
                if ($ro==false) {
                    $s .= "<td><div align=\"center\"> <a href=\"".base_url()."index_arsip.php/arsip/form_edit/".$row->idpta_arsip."\" title=\"Ubah Data\" class=\"edit\"><img src=\"".base_url()."templates/images/edit.gif\" border=\"0\"></a></div></td>".   
                          "<td><div align=\"center\"><a href=\"javascript:deleteRow(".$row->idpta_arsip.")\"><img src=\"".base_url()."templates/images/del.gif\" border=\"0\"></a></div></td>";
                }
                $s .= "</tr>'],";

                $i++;
            }
            $s = substr($s, 0, -1);
            $s .= '];';
            return $s;
        }

        function entry_form($form_id, $form_action, $arr=array()) {
        	//format tgl_lahir harus YMD
        	if (isset($arr['tgl_lahir'])) {
        		$a = explode(TGL_SEPARATOR, trimTime($arr['tgl_lahir']));
	        	$d = $a[2] * 1;
	        	$m = $a[1] * 1;
	        	$y = $a[0];
        	} else {
	        	$d = date("j");
	        	$m = date("n");
	        	$y = date("Y");
        	}
        	
        	$ret = "
			<form id=\"".$form_id."\" action=\"".$form_action."\" method=\"post\" enctype=\"multipart/form-data\">
			<fieldset >
			<table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Judul</td>
			    <td valign=\"top\" width=\"5\">:</td>
			    <td valign=\"top\"><input type=\"text\" name=\"judul\" id=\"judul\" size=\"38\" value=\"".(isset($arr['judul'])?$arr['judul']:"")."\" /></td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Kategori</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\">".combo_kategori_arsip('pta_id_kategori_arsip', (isset($arr['pta_id_kategori_arsip'])?$arr['pta_id_kategori_arsip']:""))."</td>
			  </tr>
			  <tr>
			    <td valign=\"top\">Published</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\">
			      <input type=\"radio\" name=\"published\" id=\"published\" value=\"0\" ".(isset($arr['published'])?(($arr['published']=='0')?"checked":""):"checked")." />
			      Tidak
			      <input type=\"radio\" name=\"published\" id=\"published\" value=\"1\" ".(isset($arr['published'])?(($arr['published']=='1')?"checked":""):"")." />
			      Ya
			  </tr>
			  <tr>
			    <td valign=\"top\">Deskripsi</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><textarea name=\"deskripsi\" id=\"deskripsi\" cols=\"45\" rows=\"5\">".(isset($arr['deskripsi'])?$arr['deskripsi']:"")."</textarea></td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">File</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"file\" name=\"namafile\" id=\"namafile\" size=\"20\" /> <i>".(isset($arr['namafile'])?$arr['namafile']:"")."</i></td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Tanggal Dokumen</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input id=\"tgl_dokumen\" name=\"tgl_dokumen\" class=\"date-pick dp-applied\" size=\"38\" value=\"".(isset($arr['tgl_dokumen'])?$arr['tgl_dokumen']:date('d-m-Y'))."\" /></td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Viewer</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\">".combo_kelompok_viewer('kode_viewer', '')."
			    <div id=\"div_viewer\" >".combo_jenis_admin('viewer', '', '')."</div></td>
			  </tr>";
        	if (isset($arr['created_by'])) {
        		$ret .= "
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Dibuat oleh</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><i>".(isset($arr['created_by'])?$arr['created_by']:"")."</i></td>
			  </tr>";
        	}
        	if (isset($arr['last_update'])) {
        		$ret .= "
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Terakhir di update oleh</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><i>".(isset($arr['last_update'])?$arr['last_update']:"")."</i></td>
			  </tr>";
        	}
        	$ret .= "
			  <tr>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">
			      <input class=\"tombol\" type=\"button\" name=\"btn_kembali\" id=\"btn_kembali\" value=\"<< Kembali\" />
			      <input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"   Simpan   \" />
			    </td>
			  </tr>
			</table>
			</fieldset>
			</form>
			  ";
        	
        	return $ret;
        	
/*
			  <tr>
			    <td valign=\"top\">Jenis</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\">
			      <input type=\"radio\" name=\"jenis\" id=\"jenis\" value=\"1\" ".(isset($arr['jenis'])?(($arr['jenis']=='1')?"checked":""):"checked")." />
			      Internal
			      <input type=\"radio\" name=\"jenis\" id=\"jenis\" value=\"2\" ".(isset($arr['jenis'])?(($arr['jenis']=='2')?"checked":""):"")." />
			      Eksternal
			  </tr>

 */        }

        function simpan($arr, $is_update=false, $id='') {
            if ($is_update==FALSE) {
                $this->db->insert('arsip', $arr);
            } else {
                $this->db->where('idpta_arsip', $id);
                $this->db->update('arsip', $arr);
            }
        }
        
     
     function list_publik() {
        //pta_id_kategori_arsip != 1 AND
        $this->db->where('pta_id_kategori_arsip != 1 AND pta_id_kategori_arsip != 2 AND published = 1 AND kode_viewer = 0' );
        $this->db->group_by('pta_id_kategori_arsip');
        $this->db->order_by('tgl_dokumen desc');
        $q = $this->db->get('arsip');
        $order   = array("\r\n", "\n", "\r", "'");
        $replace = ' ';
        $this->load->model('Datum_model');


        $s="";
        foreach($q->result() as $row) {
            $id = $row->pta_id_kategori_arsip;
            $kategori = Datum_model::getData('nama', 'kategori_arsip', array('idpta_kategori_arsip' => $row->pta_id_kategori_arsip));
            $j = $this->db->count_all('arsip WHERE pta_id_kategori_arsip = '.$row->pta_id_kategori_arsip.' AND kode_viewer = 0 AND published = 1');

            
            

            $s .= "<a class='menuitem submenuheader' href='#'><span>($j)</span>$kategori</a>".
                    "<div class='submenu'><ul> ";


            $this->db->where("pta_id_kategori_arsip = $id AND published = 1 AND kode_viewer = 0");
            $this->db->order_by('tgl_dokumen desc');
            $this->db->limit(10);
            $q2 = $this->db->get('arsip');

            foreach($q2->result() as $r) {
                $tgli = strtotime($r->tgl_input);
                $no = $r->idpta_arsip;
                $namafile = Datum_model::getData('namafile', 'files', array('idpta_files' => $r->pta_id_files));

                $ext = end(explode('.', $namafile));

                $s .= "<li><a href='javascript:show_detil($no)' >".str_replace($order, $replace, $r->judul)."</a></li>";
                //$s .= "<li> <div class='docinfo' > <a href='javascript:show_detil($no)'>" . str_replace($order, $replace, $r->judul)."</a></div><span style='float:right'>Hits : ".$r->hits." </span><a href='".base_url()."arsip/unduh_arsip/".$no."' target='_blank' >Download</a> </li>";
            }

            $s .= "</div></ul>";


        }

        $s .= " ";
        return $s;
    }

    function arsip_baru($where="",$limit=true) {
        if ($where!='') {
            $this->db->where($where);
        }
        if ($limit==true) {
            $this->db->limit(10);
        }
        //pta_id_kategori_arsip != 1 AND


        $order   = array("\r\n", "\n", "\r", "'");
        $replace = ' ';
        $this->load->model('Datum_model');


        $s='';

        $this->db->where("pta_id_kategori_arsip != 1 AND pta_id_kategori_arsip != 2  AND published = 1 AND kode_viewer = 0");
        $this->db->order_by('tgl_dokumen desc');

        $q2 = $this->db->get('arsip');

        foreach($q2->result() as $r) {
            $tgli = strtotime($r->tgl_input);
            $no = $r->idpta_arsip;
            $namafile = Datum_model::getData('namafile', 'files', array('idpta_files' => $r->pta_id_files));
            $kat_arsip =  Datum_model::getData('parent_id','kategori_arsip','idpta_kategori_arsip = '.$r->pta_id_kategori_arsip);   
            switch ($r->kode_viewer) {
                case VIEWER_EVERYBODY: $viewer = 'Semua';
                    break;
                case VIEWER_GRUP: $grup = Datum_model::getData('nama', 'grup', array('idpta_grup' => $r->viewer));
                    $viewer = 'Grup '.$grup;
                    break;
                case VIEWER_USER_TTT: $viewer = 'User '.$r->viewer;
            }
            $ext = end(explode('.', $namafile));

            if($kat_arsip == 4 ) {
                $unduh = '<a href="'.base_url().'index.php/arsip/unduh_arsip/'.$no.'/true" target="_blank">Download</a>';
            } else {
                $unduh = '<a href="'.base_url().'index.php/arsip/unduh_arsip/'.$no.'" target="_blank">Download</a>';
            }
            
            $s .='<style type="text/css">
                        #ars-'.$no.' {
                    height: auto;
                    width: 44%;
                    background: #000000;
                    position: absolute;
                    //top: 0;
                    margin-top:-55px;
                    margin-left:-5px;
                    -moz-opacity: 0.75;
                    -khtml-opacity: 0.75;
                    opacity: 100;
                    filter:alpha(opacity=100);
                    }


                    </style>';

            $s .= "<div class='judul'><a href='#'>".str_replace($order, $replace, $r->judul)."</a></div>
    <div class='docinfo'><span style='float:right'>Hits : ".$r->hits." | ".strftime('%d/%m/%Y',$tgli)."</span>  <br /> <a href='javascript:show_detil($no)'  >Detail</a> | $unduh </div><hr />";

            $this->db->where('idpta_files = '.$r->pta_id_files.'');
            $this->db->order_by('tgl_input desc');
            $this->db->limit(1);
            $q = $this->db->get('files');
            foreach($q->result() as $r2) {

                $s .='<div id="popup" style="display: none;"></div>
<div id="ars-'.$no.'" style="display: none;" >
<div id="popup_content" ><a href="#ars-'.$no.'" onclick="Close_Popup('.$no.');">Close</a> <br>

<table class="info-detail" width="100%" border="0" cellpadding="2" cellspacing="1">
      <tr>
        <td class="title_info">Name</td>
        <td class="dot">:</td>
        <td>'.$r->judul.'</td>
      </tr>
      <tr>
        <td class="title_info">Description</td>
        <td class="dot">:</td>
        <td>'.$r->deskripsi.'</td>
      </tr>
      <tr>
        <td class="title_info">Filename</td>
        <td class="dot">:</td>
        <td>'.$r2->namafile.'</td>
      </tr>
      <tr>
        <td class="title_info">Filesize</td>
        <td class="dot">:</td>
        <td>'.$r2->ukuran.'</td>
      </tr>
      <tr>
        <td class="title_info">Filetype</td>
        <td class="dot">:</td>
        <td>'.$r2->ekstensi.' ( '.$r2->tipe_file.')</td>
      </tr>
      <tr>
        <td class="title_info">Created On</td>
        <td class="dot">:</td>
        <td>'.$r2->tgl_input.'</td>
      </tr>
      <tr>
        <td class="title_info">Viewers</td>
        <td class="dot">:</td>
        <td>'.$viewer.'</td>
      </tr>
      <tr>
        <td class="title_info">Hits</td>
        <td class="dot">:</td>
        <td>'.$r->hits.'</td>
      </tr>
    </table>
    <br>

</div>
</div> ';
            }
        }


        $s .= ' ';


        return $s;
    }



    function arsip_detil($id="") {

        $this->load->model('Datum_model');
        $order   = array("\r\n", "\n", "\r", "'");
        $replace = ' ';
        $this->load->model('Datum_model');


        $s='';

        $this->db->where("idpta_arsip",$id);

        $q2 = $this->db->get('arsip');

        foreach($q2->result() as $r) {
            
            $kat_arsip =  Datum_model::getData('parent_id','kategori_arsip','idpta_kategori_arsip = '.$r->pta_id_kategori_arsip);
            
            switch ($r->kode_viewer) {
                case VIEWER_EVERYBODY: $viewer = 'Semua';
                    break;
                case VIEWER_GRUP: $grup = Datum_model::getData('nama', 'grup', array('idpta_grup' => $r->viewer));
                    $viewer = 'Grup '.$grup;
                    break;
                case VIEWER_USER_TTT: $viewer = 'User '.$r->viewer;
            }

            if($kat_arsip == SISKUMDANG ) {
                $unduh = '<a href="'.base_url().'index.php/arsip/unduh_arsip/'.$id.'/true" target="_blank">Download</a>';
            } else if($r->pta_id_kategori_arsip == SURAT_MoU ){
                $unduh = '';
            } else {
                $unduh = '<a href="'.base_url().'index.php/arsip/unduh_arsip/'.$id.'" target="_blank">Download</a>';
            }
            
            $this->db->where('idpta_files = '.$r->pta_id_files.'');
            $this->db->limit(1);
            $q = $this->db->get('files');
            foreach($q->result() as $r2) {
                $s .='

<table class="info-detail" width="100%" border="0" cellpadding="2" cellspacing="1">
      <tr>
        <td class="title_info">Nama</td>
        <td class="dot">:</td>
        <td>'.$r->judul.'</td>
      </tr>
      <tr>
        <td class="title_info">Deskripsi</td>
        <td class="dot">:</td>
        <td>'.$r->deskripsi.'</td>
      </tr>
      <tr>
        <td class="title_info">Nama File</td>
        <td class="dot">:</td>
        <td>'.$r2->namafile.'</td>
      </tr>
      <tr>
        <td class="title_info">Ukuran File</td>
        <td class="dot">:</td>
        <td>'.$r2->ukuran.'</td>
      </tr>
      <tr>
        <td class="title_info">Tipe File</td>
        <td class="dot">:</td>
        <td>'.$r2->ekstensi.' ( '.$r2->tipe_file.')</td>
      </tr>
      <tr>
        <td class="title_info">Tanggal Dibuat</td>
        <td class="dot">:</td>
        <td>'.$r2->tgl_input.'</td>
      </tr>
      <tr>
        <td class="title_info">Viewers</td>
        <td class="dot">:</td>
        <td>'.$viewer.'</td>
      </tr>
      <tr>
        <td class="title_info">Hits</td>
        <td class="dot">:</td>
        <td>'.$r->hits.' | '.$unduh.'</td>
      </tr>

    </table>
 ';
            }
        }


        $s .= ' ';


        return $s;
    }

    function kirim_email() {
        $this->load->library('email');
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = "html";
        $config['protocol'] = "smtp";
        $config['smtp_port'] = "25";
        $config['smtp_host'] = "mx1.itb.ac.id";
        $config['smtp_user'] = "admin@arsip.itb.ac.id";
        $config['smtp_pass'] = "123456";

        $this->email->initialize($config);

   
            
            $isi = "<h3>Notifikasi MoU</h3>  ".br(3).
                    $deskripsi.br(2).
                    "bila lampiran MoU ini tidak dapat diunduh coba klik tautan <a href=\"".base_url()."index.php/arsip/unduh_arsip/$id \" >ini</a>";
            
            $isi .= br(2)."<a href=\"".base_url()."index.php/mou \" >tautan ke arsip ini</a>";                      
     
    
            $email = $r->email;
            $this->email->from($this->session->userdata('email'));
            $this->email->to($email);
            $this->email->subject($judul);
            $this->email->message($isi);
            

            if ($this->email->send()) {
                $arr['email_sent'] = '1';
            } else {
                $arr['email_sent'] = '0';
            }
           
            $this->db->where('idpta_arsip', $id);
            $this->db->update('arsip', $arr);
 
    }   
        
	}
?>